package com.lab.converter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CurrencyConverter extends Activity{
    
	/** Called when the activity is first created. */
	public double ex_rate1 = 0;
	public double ex_rate2 = 0; 
	public Spinner currency_spinner1;
	public Spinner currency_spinner2;
	public ArrayAdapter c1adapter;
	public ArrayAdapter c2adapter;
	public EditText amount1;
	public EditText amount2;
	public Button convert_button;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // populate value for spinners
        currency_spinner1 = (Spinner) findViewById(R.id.selected_currency1);
        currency_spinner2 = (Spinner) findViewById(R.id.selected_currency2);
        
        c1adapter = ArrayAdapter.createFromResource( this, R.array.currency_list, android.R.layout.simple_spinner_item); 
        c1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        currency_spinner1.setAdapter(c1adapter);
        
        c2adapter = ArrayAdapter.createFromResource( this, R.array.currency_list, android.R.layout.simple_spinner_item); 
        c2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        currency_spinner2.setAdapter(c2adapter);  
        
        // get amount to convert
        amount1 = (EditText) findViewById(R.id.amount1);
        amount2 = (EditText) findViewById(R.id.amount2);
        
        // set convert button to convert currency
        convert_button = (Button) findViewById(R.id.convert);
        convert_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
            	// get the selected currencies and value from the interface
        		double val = Double.parseDouble(amount1.getText().toString());
        		double rate1 = getExchangeRate(currency_spinner1.getSelectedItemPosition());
        		double rate2 = getExchangeRate(currency_spinner2.getSelectedItemPosition());
        		
        		if(val == 0){
        			Toast.makeText(view.getContext(), "Please select an amount other than 0 to convert" ,Toast.LENGTH_LONG).show();
        		}else{        			
        			// display the converted value in amount 2 edit text box
        			amount2.setText(Double.toString(calculateConversion(rate1, rate2, val)));
        		}
        		
            }
        });

    }
    
    // convert value from one currency to another and keep it to two decimal
    // exchange rate is calculated based on the formula amt/rate1*rate2;
    public double calculateConversion(double rate1, double rate2, double amt){
    	double newVal = amt/rate1*rate2;
        long intermediateVal = (int)Math.round (newVal * 100); // round up and truncates more than 2 decimal places
    	newVal = intermediateVal / 100.0;   	
    	return newVal;
    }
    
    // get exchange rate base on selected currency
    private double getExchangeRate(int currency){
    	double exchange_rate = 0;
    	if(currency == 0){ // United States dollar (USD)
    		exchange_rate = 1.422;
    	}else if(currency == 1){ // Euro (EUR)
    		exchange_rate = 1;
    	}else if(currency == 2){ // United Kingdom Pound (GBP)
    		exchange_rate = 0.87;
    	}else if(currency == 3){ // Taiwanese New Dollar (TWD) 
    		exchange_rate = 42.07;
    	}else if(currency == 4){ // Thai Baht (THB)
    		exchange_rate = 43.03;
    	}else if(currency == 5){// Canadian Dollar (CAD)
    		exchange_rate = 1.40;
    	}else if(currency == 6){ // Japanese Yen (JPY)
    		exchange_rate =  115.32;
    	}else if(currency == 7){ // Mexican Peso (MXN)
    		exchange_rate =  17.05;
    	}else if(currency == 8){ // Nepalese Rupee (NPR)
    		exchange_rate =  102.53;
    	}else{
    		exchange_rate =  2663.68;// Colombian Peso (COP)
    	}    	
    	
    	return exchange_rate;
    }
}